package com.example.webbasic.filter;

import java.io.IOException;
import java.util.function.Consumer;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

import org.springframework.core.annotation.Order;

/**
 * MyFilter2
 * WebFilter注解 的 过滤器操作先执行 
 * @author ben
 * @date 2021-11-06 17:04:08 CST
 */
@WebFilter(filterName = "myFilter2", urlPatterns = {"/a/*", "/b/*", "/s/*"})
// 调整优先级 无用
//@Order(100)
public class MyFilter2 implements Filter {

	private static Consumer<Object> cs = System.out::println;
	
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		cs.accept("MyFilter2 过滤器初始化：" + MyFilter2.class);
	}
	
	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
			throws IOException, ServletException {
		// 可以做什么呢？
		cs.accept("MyFilter2 req=" + req + ", param.key=" + req.getParameter("key"));
		req.setAttribute("key", "set key at MyFilter2");
		cs.accept("MyFilter2 执行过滤操作...scheme=" + req.getScheme());
		chain.doFilter(req, resp);
	}
	
	@Override
	public void destroy() {
		cs.accept("MyFilter2 过滤器销毁：" + MyFilter2.class);
	}

}
